package cn.jobstart.service.impl;

import cn.jobstart.common.NumberUtil;
import cn.jobstart.common.SkMap;
import cn.jobstart.common.StringUtil;
import cn.jobstart.dao.IJobDao;
import cn.jobstart.enums.JobNextFlagEnum;
import cn.jobstart.model.Job;
import cn.jobstart.modelExt.JobExt;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.pubInter.BaseSearch;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.JobSearchExt;
import cn.jobstart.service.IJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author sunke
 * @Date 2023-09-15 14:55:16
 * @description JobService      职位名称
 */
@Service(value = "jobService")
@Slf4j
public class JobServiceImpl extends AbstractBaseService<JobExt, JobSearchExt, String> implements IJobService {

    @Resource(name = "jobDao")
    private IJobDao jobDao;

    @Override
    public IBaseDao<JobExt, JobSearchExt, String> getDao() {
        return jobDao;
    }


    /**
     * 更新是否有子级
     *
     * @param jobId
     * @param nextFlag
     */
    @Override
    public void updateNextFlagByJobId(String jobId, int nextFlag) {

        try {
            if (StringUtil.isNullOrEmpty(jobId)) {
                return;
            }
            if (nextFlag == -1) {

                updateDataById(SkMap.getInstance().addObjToMap(Job.NEXT_FLAG,
                        StringUtil.isNotObjEmpty(getSubListByJobId(jobId)) ? JobNextFlagEnum.Y.getType().intValue() :
                                JobNextFlagEnum.N.getType().intValue()), jobId);

                return;
            }

            updateDataById(SkMap.getInstance().addObjToMap(Job.NEXT_FLAG, nextFlag), jobId);

        } catch (Exception e) {

        }

    }

    @Override
    @Transactional
    public int delete(String id) throws SkException {

        JobExt job = getDetail(id);

        //删除时，他的下级，就变成和删除的同级，所以当前先减一个，
        int level = NumberUtil.parseInt(job.getLevel(), 1) - 1;
        updateDataBySearch(SkMap.getInstance().addObjToMap(Job.UP_ID, job.getUpId()), JobSearchExt.getInstance().setUpIdEq(id));

        updateAllSubLevelByJobId(job.getUpId(), level, false);


        return super.delete(id);
    }

    /**
     * 得到所有
     *
     * @return
     */
    @Override
    public List<JobExt> getTopList() {

        try {
            JobSearchExt search = JobSearchExt.getInstance();
            search.setTopFlag(1);
            return getAllList(search);
        } catch (SkException e) {
            return null;
        }
    }


    /**
     * 更新所有下级等级
     *
     * @param jobId
     * @param currentLevel
     * @param upSelf       是否更新自己
     */
    @Override
    public void updateAllSubLevelByJobId(String jobId, int currentLevel, boolean upSelf) {
        try {
            JobExt job = getDetail(jobId);
            updateAllSubLevelByJob(job, currentLevel, upSelf);


        } catch (SkException e) {

        }

    }

    @Override
    public void updateAllSubLevelByJob(JobExt job, int currentLevel, boolean upSelf) {
        try {
            if (upSelf) {
                job.setLevel(currentLevel);
                update(job);
            }
            currentLevel = currentLevel + 1;
            List<JobExt> subList = getSubListByJobId(job.getId());
            if (StringUtil.isNullOrEmpty(subList)) {
                return;
            }
            int finalCurrentRightsLevel = currentLevel;
            subList.stream().forEach(jobExt -> {
                updateAllSubLevelByJob(jobExt, finalCurrentRightsLevel, true);
            });


        } catch (SkException e) {

        }
    }


    /**
     * 树型
     *
     * @param level 需要几级数据
     * @return
     */
    @Override
    public List<JobExt> tree(int level) {

        try {
            JobSearchExt search=JobSearchExt.getInstance();
            search.setLevelLess(level);
            List<JobExt> jobList=getAllList(search);
            if(StringUtil.isNullOrEmpty(jobList)){
                return null;
            }

            List<JobExt> topList =  jobList.stream().filter(item->StringUtil.isNullOrEmpty(item.getUpId())).collect(Collectors.toList());
            if(StringUtil.isNullOrEmpty(topList)){
                return null;
            }

            topList.stream().forEach(item -> {
                treeByJob(item, level,jobList);
            });



            return topList;



        } catch (SkException e) {

            return null;
        }




    }




    /**
     * 树型
     *
     * @param level 需要几级数据
     * @return
     */
    //@Override
    public List<JobExt> tree1(int level) {
        List<JobExt> topList = getTopList();
        if (StringUtil.isNullOrEmpty(topList)) {
            return null;
        }
      /*  if (level == 1) {
            return topList;
        }*/

        topList.stream().forEach(item -> {
            treeByJob(item, level);

        });


        return topList;
    }


    private void treeByJob(JobExt job, int level,List<JobExt> jobList) {

        try {
            if (StringUtil.isNullOrEmpty(job)) {
                return;
            }
            String upId=job.getId();


            List<JobExt> subList = jobList.stream().filter(item->upId.equalsIgnoreCase(item.getUpId())).collect(Collectors.toList());

           /* if (StringUtil.isNotObjEmpty(subList)) {
                job.setNextFlag(1);
            } else {
                job.setNextFlag(0);
            }*/

            //说明当前级已经够了
            if (level != -1 && NumberUtil.parseInt(job.getLevel()) >= level) {
                return;
            }

            if (StringUtil.isNullOrEmpty(subList)) {
                return;
            }


            job.setSubList(subList);

            subList.stream().forEach(item -> {

              /*  if (level != -1 && NumberUtil.parseInt(item.getLevel()) > level) {
                    return;
                }*/

                treeByJob(item, level,jobList);

            });


        } catch (Exception e) {

        }

    }


    /**
     * 下级
     *
     * @param job
     * @param level
     * @throws SkException
     */
    @Override
    public void treeByJob(JobExt job, int level) {
        try {
            if (StringUtil.isNullOrEmpty(job)) {
                return;
            }


            List<JobExt> subList = getSubListByJobId(job.getId());

           /* if (StringUtil.isNotObjEmpty(subList)) {
                job.setNextFlag(1);
            } else {
                job.setNextFlag(0);
            }*/

            //说明当前级已经够了
            if (level != -1 && NumberUtil.parseInt(job.getLevel()) >= level) {
                return;
            }

            if (StringUtil.isNullOrEmpty(subList)) {
                return;
            }


            job.setSubList(subList);

            subList.stream().forEach(item -> {

              /*  if (level != -1 && NumberUtil.parseInt(item.getLevel()) > level) {
                    return;
                }*/

                treeByJob(item, level);

            });


        } catch (SkException e) {

        }
    }

    /**
     * 得到的下级树结构
     *
     * @param jobId
     * @return
     * @throws SkException
     */
    @Override
    public JobExt treeByJobId(String jobId, int level) throws SkException {
        if (StringUtil.isNullOrEmpty(jobId)) {
            return null;
        }
        JobExt job = getDetail(jobId);
        List<JobExt> subList = getSubListByJobId(jobId);
        job.setSubList(subList);
        if (StringUtil.isNotObjEmpty(subList)) {
            subList.stream().forEach(item -> {

                treeByJob(item, level);

            });

        }


        return job;
    }

    /**
     * 得到某一个级的下一级
     *
     * @param jobId
     * @return
     * @throws SkException
     */
    @Override
    public List<JobExt> getSubListByJobId(String jobId) throws SkException {
        JobSearchExt search = JobSearchExt.getInstance();
        search.setUpIdEq(jobId);
        search.setOrderBy(Job.SORT_NUM);
        search.setOrderDesc(BaseSearch.ASC_ORDER);

        return getAllList(search);
    }
}